<template>
  <div>
    <dv-border-box-10
      :style="{ '--right': right + 'px' }"
      backgroundColor="#00003bc1"
      :color="['#4789e3', '#65bfe5']"
      class="right-row-bg"
    >
      <div class="right-row-box">
        <div class="content">
          <div class="tools">
            <dv-decoration-7 :dur="2" class="title"
              >&nbsp;<font size="5">图层</font>&nbsp;</dv-decoration-7
            >
            <div class="tool">
              <i class="el-icon-close" @click="removeFn"></i>
            </div>
          </div>
          <div class="box-content">
            <h3 class="title">平台图层</h3>
            <div class="card">
              <div v-for="item in mLayers.al" :key="item.uid" class="card-item">
                <div class="content">
                  <template>
                    <img :src="imageMap[item.type]" />
                  </template>
                  <span class="tmp" :title="item.title">{{ item.title }}</span>
                </div>
                <div class="tools">
                  <el-switch
                    v-model="item.visible"
                    active-color="#1dc1f5"
                    size="mini"
                    @change="changeFn($event, item.layer)"
                  >
                  </el-switch>
                  <el-button
                    :disabled="!item.edit"
                    size="mini"
                    type="success"
                    icon="el-icon-edit"
                    circle
                  ></el-button>
                  <el-button
                    :disabled="!item.delete"
                    size="mini"
                    type="info"
                    icon="el-icon-delete"
                    circle
                  ></el-button>
                </div>
              </div>
            </div>
            <h3 class="title">用户图层</h3>
            <div class="card" v-if="mLayers.ul.length > 0">
              <div v-for="item in mLayers.ul" :key="item.uid" class="card-item">
                <template v-if="item.uid">
                  <div class="content">
                    <template>
                      <img :src="imageMap[item.type]" />
                    </template>
                    <span class="tmp" :title="item.title">{{
                      item.title
                    }}</span>
                  </div>
                  <div class="tools">
                    <el-switch
                      v-model="item.visible"
                      active-color="#1dc1f5"
                      size="mini"
                      @change="changeFn($event, item.layer)"
                    >
                    </el-switch>
                    <el-button
                      :disabled="!item.edit"
                      size="mini"
                      type="success"
                      icon="el-icon-edit"
                      circle
                      @click="editFn(item.layer)"
                    ></el-button>
                    <el-button
                      :disabled="!item.delete"
                      size="mini"
                      type="info"
                      icon="el-icon-delete"
                      circle
                      @click="deleteFn(item)"
                    ></el-button>
                  </div>
                </template>
              </div>
            </div>
            <template v-else>
              <p style="color: red">暂无用户图层或要素层...</p>
            </template>
          </div>
        </div>
      </div>
    </dv-border-box-10>
    <dv-border-box-12
      v-show="dialogVisible"
      class="dialog"
      style="height: 150px !important"
    >
      <p>配置项</p>
      <span v-if="currentEditLayer"
        >正在修改图层：{{ currentEditLayer.get("title") }}</span
      >
      <p></p>
      <div class="dialog-commit">
        <input type="button" value="结束修改" @click="endModify" />
      </div>
    </dv-border-box-12>
  </div>
</template>

<script>
import { Snap, Modify } from "ol/interaction.js";
export default {
  name: "Test1LeftRow",
  data() {
    return {
      right: -500,
      flag: "M2",
      visible: true,
      mLayers: [],
      modify: null,
      snap: null,
      dialogVisible: false,
      currentEditLayer: null,
    };
  },
  props: {
    Map: {
      type: Object,
      requered: true,
    },
  },
  mounted() {},
  computed: {
    getMenuFlag() {
      return this.$store.state.activeMenu;
    },
    getLayeres() {
      return this.$store.getters.getLayeres;
    },
    imageMap() {
      return {
        LineString: require("../../assets/icon/line.png"),
        Point: require("../../assets/icon/point-01.png"),
        Polygon: require("../../assets/icon/mian.png"),
        Layer: require("../../assets/icon/layers-fill.png"),
        Circle: require("../../assets/icon/circle1.png"),
        PointCollection: require("../../assets/icon/PointCollection.png"),
      };
    },
  },
  watch: {
    getMenuFlag: {
      handler(newValue, oldValue) {
        // console.log(newValue, oldValue);
        this.right = -500;
        if (newValue === this.flag) {
          this.right = 0;
        }
      },
      deep: true,
      immediate: true,
    },
    getLayeres: {
      handler(newValue, oldValue) {
        this.mLayers = newValue;
      },
      deep: true,
      immediate: true,
    },
  },

  methods: {
    removeFn() {
      console.log("removeFn");
      this.right = -500;
    },
    changeFn($event, layer) {
      layer.set("visible", $event);
    },
    deleteFn(item) {
      this.Map.removeLayer(item.layer);
      if (item.children.length > 0) {
        var children = item.children;
        for (let i = 0; i < children.length; i++) {
          children[i].parentNode.removeChild(children[i]);
        }
      }

      // 修改所数据集的加载状态
      let selections = this.$store.state.selections;
      for (let i = 0; i < selections.length; i++) {
        if (selections[i].id == item.id) {
          selections[i].load = false;
        }
      }
      this.$store.commit("setSelections", selections);

      let layeres = this.$store.getters.getLayeres;
      layeres.ul = layeres.ul.filter((p) => p.uid != item.uid);
      this.$store.commit("setLayeres", layeres);
    },
    editFn(layer) {
      this.currentEditLayer = layer;
      layer.setOpacity(0.8);

      let source = layer.getSource();

      this.modify = new Modify({
        source,
      });

      this.snap = new Snap({
        source,
      });

      this.Map.addInteraction(this.modify);
      this.Map.addInteraction(this.snap);

      this.dialogVisible = true;
    },
    endModify() {
      this.Map.removeInteraction(this.modify);
      this.Map.removeInteraction(this.snap);

      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
.right-row-bg {
  position: absolute;
  top: 0;
  right: var(--right);
  bottom: 0;
  width: 20%;
  margin: 5px;
  z-index: 9;
  backdrop-filter: saturate(150%) blur(8px);
  -webkit-backdrop-filter: saturate(150%) blur(8px);
  animation: run 1s linear;
  color: #1dc1f5;
}

@keyframes run {
  0% {
    left: -100%;
  }

  100% {
    left: 0;
  }
}

.right-row-box {
  position: absolute;

  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  // color: #4789e3;
  // color: #3f96a5;

  .border-box {
    width: 95%;
    height: 150px;
  }

  // .tolbar {
  //     width: 100%;
  //     height: 5%;
  // }

  .content {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: flex-start;
    width: 100%;
    height: 100%;
  }

  .box-content:nth-child(1n) {
    flex: 2;
  }

  .box-content:nth-child(2n) {
    flex: 1;
  }

  .box-content {
    .title {
      text-align: left;
      margin: 10px;
      padding-left: 5px;
      border-left: 5px #139fc0 solid;
    }

    .card {
      margin: 5px;
      box-sizing: border-box;
      padding-left: 10px;
      .card-item {
        width: 100%;
        height: 45px;
        box-sizing: border-box;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: space-between;
        align-items: center;
        margin-top: 5px;
        padding: 5px;
        transition: 0.3s linear;

        &:hover {
          border-left: 4px #4789e3 solid;
        }
        div {
          display: flex;
          flex-direction: row;
          align-items: center;
        }
        .content {
          justify-content: flex-start;
          img {
            width: 30px;
            margin-right: 10px;
          }
        }
        .tools {
          justify-content: flex-end;
          i,
          .el-switch {
            font-size: 22px !important;
            cursor: pointer;
            margin-right: 8px;
          }
        }
      }
    }
  }
}

.tools {
  // display: flex;
  // flex-direction: row;
  // flex-wrap: nowrap;
  // justify-content: space-around;
  // align-items: center;
  position: relative;
  width: 100%;
  margin-bottom: 5px;

  .title {
    width: 100%;
    height: 30px;
  }

  .tool {
    position: absolute;
    right: 10px;
    top: 5px;
    background: #4687e240;
  }
}

.el-descriptions {
  background-color: transparent !important;
  color: #1dc1f5 !important;
  font-size: 18px;
}
.el-switch__core {
  height: 14px !important;
}
.el-switch__core:after {
  height: 14px !important;
}
.margin-left {
  margin-left: 10px;
}
</style>

<style>
.tmp {
  white-space: nowrap; /*强制单行显示*/
  text-overflow: ellipsis; /*超出部分省略号表示*/
  overflow: hidden; /*超出部分隐藏*/
  width: 100px; /*设置显示的最大宽度*/
  display: inline-block;
}
</style>
